package com.ilvyou.api.controller.finance;

import com.ilvyou.api.controller.BaseController;
import com.ilvyou.api.bean.result.Result;
import com.ilvyou.api.service.BillService;
import com.ilvyou.core.dao.Criteria;
import com.ilvyou.core.dao.Query;
import com.ilvyou.core.util.StringUtil;
import com.ilvyou.data.lhf.AlipayLogEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by GuanYuCai on 2016/9/21 0021.
 */
@Controller
public class BillController extends BaseController {

    @Autowired
    private BillService billService;

    @RequestMapping("/bill/list")
    @ResponseBody
    public Result list(@RequestParam String token, String month, String type, int pageno, int pagesize) throws Exception{
        int iMonth = getMonth(month);
        if (iMonth == 0){
            return fail("月份格式不正确");
        }

        String flow = "";
        if ("TransferIn".equals(type)){
            type = "Transfer";
            flow = "In";
        }else if ("TransferOut".equals(type)){
            type = "Transfer";
            flow = "Out";
        }

        Criteria criteria = new Criteria("actid=?", getUserId(token))
                .add("tradetype=?", type)
                .add("tradeflow=?", flow);
        if ("trade".equals(type)){
            criteria.add("tradetype='Receive' or tradetype='Pay'");
        }

        if (StringUtil.isEmpty(month)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            Calendar calendar = Calendar.getInstance();
            int d1 = Integer.parseInt(sdf.format(calendar.getTime()));
            calendar.add(Calendar.MONTH, -1);
            int d2 = Integer.parseInt(sdf.format(calendar.getTime()));
            calendar.add(Calendar.MONTH, -1);
            int d3 = Integer.parseInt(sdf.format(calendar.getTime()));
            criteria.add("periodmonth=? or periodmonth=? or periodmonth=?", d1, d2, d3);
        }else{
            criteria.add("periodmonth=?", iMonth);
        }

        Query query = new Query(AlipayLogEntity.class, "logid,tradetype as type,tradedate,tradeamount as amount," +
                    "tradestatus as status,tradeflow,bppcompanyname as companyname")
                .where(criteria)
                .order("tradedate desc")
                .page(pageno, pagesize);
        List<Map<String,Object>> list = billService.getMapList(query);
        //遍历并赋值
        for (Map<String, Object> map : list) {
            if("Recharge".equals(map.get("type").toString()) && "1".equals(map.get("status").toString())){
                continue;
            }

            String name = billService.getTradeName((String) map.get("type"), (Long) map.get("logid"));
            map.put("tradename", StringUtil.isEmpty(name) ? map.get("companyname") : name);
        }

        return success(list);
    }

    @RequestMapping("/bill/detail")
    @ResponseBody
    public Result detail(@RequestParam String token, @RequestParam long billid) throws Exception{
        AlipayLogEntity log = billService.getByPK(AlipayLogEntity.class, billid);
        if (log == null){
            return fail("没有该账单");
        }

        return success(billService.getBillDetail(log.getTradetype(), billid, getUserId(token)));
    }

    @RequestMapping("/bill/monthBill")
    @ResponseBody
    public Result monthBill(@RequestParam String token, String month) throws Exception{
        int iMonth = getMonth(month);
        if (iMonth == 0){
            return fail("月份格式不正确");
        }

        return success(billService.getMonthBill(getUserId(token), iMonth));
    }

    private int getMonth(String month){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        if (!StringUtil.isEmpty(month)){
            try{
                date = sdf.parse(month);
            }catch (Exception e){
                return 0;
            }
        }
        return Integer.parseInt(sdf.format(date));
    }
}
